package com.fly.user.service.impl;

import com.fly.common.idworker.IdWorker;
import com.fly.common.model.UserEntity;
import com.fly.user.dao.UserMapper;
import com.fly.user.service.PropagationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description 测试事务传播机制
 * @author: fly
 * @date: 2018/12/6 18:50
 */
@Service
public class PropagationServiceImpl implements PropagationService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 测试事务传播机制
     */
    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.SUPPORTS)
    public void testPropagationA() {
        UserEntity u1 = new UserEntity();
        u1.setUserId(idWorker.nextId());
        u1.setRealName("A1");
        userMapper.insert(u1);
        UserEntity u2 = new UserEntity();
        u2.setUserId(idWorker.nextId());
        u2.setRealName("A2");
        userMapper.insert(u2);
    }

    /**
     * 测试事务传播机制
     */
    @Override
    public void testPropagationB() {
        UserEntity u1 = new UserEntity();
        u1.setUserId(idWorker.nextId());
        u1.setRealName("B");
        userMapper.insert(u1);
    }
}
